﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessObjects.BO_GetObjects;
using LibraryO2S;
using BusinessObjects;

public partial class controls_admin_AdvPicture : Pages_Base
{
    protected void Page_Load(object sender, EventArgs e)
    {
        base.removeHGC(divMessage);
        base.removeHGC(divResult);
        if (Request["t"] == null)
            if (Request["m_id"] != null)
                ddlModule.SelectedValue = Request["m_id"].ToString();
        if (!IsPostBack)
            loadAdv();
    }
    private void loadAdv()
    {
        try
        {
            base.OpenConnection();
            DataTable objDT;
            if (Request["t"] != null)
            {
                ddlModule.Visible = false;
                objDT = BO_Ads_Picture.SelectByModule(Convert.ToInt32(Request["m_id"]));
                base.bindRepeater(rptPicture, objDT, divMessage);
            }
            else
            {
                objDT = BO_Ads_Picture.SelectByType(base.Cur_page, base.Page_size, Convert.ToInt32(ddlModule.SelectedValue));
                base.bindRepeater(rptPicture, objDT, pagerContainer, divMessage, divResult);
            }
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi trong quá trình xử lý<br />" + ex.Message + ex.Source);
        }
        finally { base.CloseConnection(); }
    }
    protected void rptPicture_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        try
        {
            base.OpenConnection();
            Button btnID = (Button)e.Item.FindControl("btnID");
            string commandName = e.CommandName;
            int picture_id = Convert.ToInt32(btnID.CommandArgument);
            if (commandName.Equals("del"))
            {
                BO_Ads_Picture.Delete(picture_id);
                divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Xóa thành công .");
            }
            loadAdv();
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi: <br />" + ex.Message + ex.Source);
        }
        finally { base.CloseConnection(); }
    }
    protected void ddlModule_SelectedIndexChanged(object sender, EventArgs e)
    {
        string url = base.removeRequest();
        if (Request["m_id"] != null)
        {
            int conti = url.LastIndexOf("m_id=");
            string start = url.Substring(0, url.LastIndexOf("&m_id="));
            string end = url.Substring(conti, url.Length - conti);
            conti = end.IndexOf("&");
            if (conti > -1)
                url = start + end.Substring(conti, end.Length - conti);
            else
                url = start;
        }
        Response.Redirect(url + "&m_id=" + ddlModule.SelectedValue);
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx?module=AdvPictureAction");
    }
}
